<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <!-- 靠右的按钮 -->
        <el-row type="flex" justify="end">

          <el-button type="primary" size="small" @click="addPerm(1,'0')">添加权限</el-button>
        </el-row>

        <!-- 表格 -->
        <el-table border :data="permList" row-key="id" default-expand-all="">
          <el-table-column align="center" label="名称" prop="name" />
          <el-table-column align="center" label="标识" prop="code" />
          <el-table-column align="center" label="描述" prop="description" />
          <el-table-column align="center" label="操作">
            <template #default="{row}">
              <el-button type="text" @click="addPerm(2,row.id)">添加</el-button>
              <el-button type="text" @click="updatePerm(row.id)">编辑</el-button>
              <el-button type="text" @click="delBtn(row.id)">删除</el-button>
            </template>
          </el-table-column>

        </el-table>
      </el-card>
      <el-dialog title="权限" :visible="isShowDialog" @close="btnCancel">
        <el-form ref="addPerm" :model="formData" label-width="80px">
          <el-form-item label="权限名称">
            <el-input v-model="formData.name" />
          </el-form-item>
          <el-form-item label="权限标识">
            <el-input v-model="formData.code" />
          </el-form-item>
          <el-form-item label="权限描述">
            <el-input v-model="formData.description" />
          </el-form-item>
          <el-form-item label="开启">
            <el-switch v-model="formData.enVisible" active-value="1" inactive-value="0" />
          </el-form-item>
        </el-form>
        <template #footer>
          <el-button size="small" type="primary" @click="btnOk">确定</el-button>
          <el-button size="small" @click="btnCancel">取消</el-button>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getPermList, getPermListById, updatePermList, addPermList, delPermList } from '@/api/permission'
import { listTotreeData } from '@/utils/index'
export default {
  data() {
    return {
      permList: [],
      isShowDialog: false,
      formData: {
        name: '', // 名称
        code: '', // 标识
        description: '', // 描述
        type: '', // 类型 该类型 不需要显示 因为点击添加的时候已经知道类型了
        pid: '', // 因为做的是树 需要知道添加到哪个节点下了
        enVisible: '1' // 开启
      }
    }
  },
  created() {
    this.getPermission()
  },
  methods: {
    addPerm(type, id) {
      this.formData.type = type
      this.formData.pid = id
      this.isShowDialog = true
    },
    async delBtn(id) {
      await this.$confirm('确认删除')
      await delPermList(id)
      this.$message.success('删除成功')
      this.getPermission()
    },
    async btnOk() {
      if (this.formData.id) {
        await this.$refs.addPerm.validate()
        await updatePermList(this.formData)
        this.$message.success('更新成功')
        this.isShowDialog = false
        this.getPermission()
        return
      }
      await this.$refs.addPerm.validate()
      await addPermList(this.formData)
      this.$message.success('添加成功')
      this.isShowDialog = false
      this.getPermission()
    },
    async getPermission() {
      this.permList = listTotreeData(await getPermList(), '0')
    },
    async updatePerm(id) {
      this.formData = await getPermListById(id)
      this.isShowDialog = true
    },
    btnCancel() {
      this.formData = {
        name: '', // 名称
        code: '', // 标识
        description: '', // 描述
        type: '', // 类型 该类型 不需要显示 因为点击添加的时候已经知道类型了
        pid: '', // 因为做的是树 需要知道添加到哪个节点下了
        enVisible: '1' // 开启
      }
      this.isShowDialog = false
    }
  }
}
</script>

<style>

</style>
